﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace homework3
{
    public interface shapeOp
    {
        double getArea();
        bool isLegal();
    }
    public abstract class Shape:shapeOp
    {
        public double area;
        public virtual double getArea() { return area; }
       public virtual bool isLegal() { return true; }
    }
    public class Rectangle : Shape //长方形
    {
        public double length { get; set; }
        public double width { get; set; }
        public Rectangle(double length, double width)
        {
            this.length = length;
            this.width = width;
            base.area=getArea();
        }
        public override double getArea() {
            if(isLegal())
            return length * width;
            else { Console.WriteLine("ILEGAL SHAPE!");return 0; }
        }
        public override bool isLegal()
        {
            return length>0 && width>0;
        }
    } public class Square : Shape   //正方形
    {
        public double length { get; set; }
        public Square(double length)
        {
            this.length = length;
            base.area = getArea();
        }
        public override double getArea()
        {
            if(isLegal())
            return length * length;
            else { Console.WriteLine("ILEGAL SHAPE!");return 0; }
        }
        public override bool isLegal()
        {
            return length>0 ;
        }
    }public class Triangle : Shape  //三角形
    {
        public double a { get; set; } public double b { get; set; } public double c { get; set; }
        public Triangle(double a, double b, double c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
            base.area = getArea();
        }
        public override double getArea()
        {
            if (isLegal())
            {
                double p = (a + b + c) / 2;
                return Math.Sqrt(p * (p - a) * (p - b) * (p - c));
            }
            else { return 0; }
        }
        public override bool isLegal()
        {
            if (a <= 0 || b <= 0||c<=0) return false;
            else
            {
                if(a+b>c&&a+c>b&&b+c>a) return true;
                else return false;
            }
        }
    }
    public static class ShapeFactory
    {
        private static Random _random = new Random();

        // 创建随机形状
        public static Shape CreateRandomShape()
        {
            int shapeType = _random.Next(0, 3); 

            switch (shapeType)
            {
                case 0: // 长方形
                    double length = _random.Next(1, 10);
                    double width = _random.Next(1, 10);
                    return new Rectangle(length, width);
                case 1: // 正方形
                    double side = _random.Next(1, 10);
                    return new Square(side);
                case 2:
                    Triangle t;
                    do
                    {
                        double sideA = _random.Next(1, 10);
                        double sideB = _random.Next(1, 10);
                        double sideC = _random.Next(1, 10);
                        t = new Triangle(sideA, sideB, sideC);
                    } while (t.isLegal());
                    return t;
                default:
                    throw new InvalidOperationException("未知的形状类型。");
            }
        }
    }
    internal class Shapes
    {
        
        static void Main(string[] args)
        {
            double area = 0;
            for (int i = 0; i < 10; i++)
            {
                area += ShapeFactory.CreateRandomShape().area;
            }
            Console.WriteLine(area);
        }
    }
}
